/**
 * Created by python on 18-12-10.
 */
/**
 * Created by python on 18-12-10.
 */
var vm = new Vue({
    el: '#app1',
    data: {
        delimiters: ['{{', '}}'],
        host: HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        //基本信息
        real_name: '',
        real_avatar: '',
        mobile: '',
        city: '',
        skills: '',
        personal_web: '',
        birth_time: '',
        email: '',
        company: '',
        address: '',
        gender: '',
        //经历表
        u_company: '',
        u_city: '',
        position: '',
        s_time: '',
        e_time: '',
        u_skill: '',
        desc: '',
        ex_id: '',
        experiences: '',
        //教育表
        educations: '',
        u_education: '',
        u_school: '',
        study: '',
        s_time1: '',
        e_time1: '',
        ed_id: '',
        u_skills: '',
        desc1: '',
        set_info: true,
        set_jobs: true,
        set_educations: true,
        set_person: true,
        sets_skill: true,
        histories: []
    },
    mounted: function () {
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.host + '/user/', {
                // 向后端传递JWT token的方法
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                // withCredentials: true,
                responseType: 'json',
            })
                .then(response => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.address = response.data.address;
                    this.mobile = response.data.mobile;
                    this.birth_time = response.data.birth_time;
                    this.city = response.data.city;
                    this.school = response.data.school;
                    this.personal_web = response.data.personal_web;
                    this.email = response.data.email;
                    this.real_avatar = response.data.real_avatar;
                    this.company = response.data.company;
                    this.real_name = response.data.real_name;
                    // this.jobs = response.data.jobs;
                    // this.tech = response.data.tech;
                    this.gender = response.data.gender;
                    this.skills = response.data.skills.split("," || ";");
                    this.experiences = response.data.experiences;
                    this.educations = response.data.educations;//关联用户表，当请求用户数据时，把经历表和教育表的数据页返回。


                })
                .catch(error => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = '/person-loginsign.html?next=/person-myfile.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-myfile.html';
        }


    }

    ,
    methods: {

        fileImage: function (e) {
            var file = e.target.files[0];
            var imgSize = file.size / 1024 / 1024;
            if (imgSize > 2) {
                alert('请上传大小不要超过2MB的图片')
            }
            else {
                var reader = new FileReader();
                reader.readAsDataURL(file); // 读出 base64
                alert(file);
                reader.onloadend = function () {

                    var dataURL = reader.result;

                    axios.post(this.host + '/user/',
                        {
                            avatar: dataURL
                        }
                        ,
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token
                            },
                            responseType: 'json'
                        }
                    )
                        .then(response => {
                            this.avatar = response.data.avatar;
                            alert("11")
                        })
                    // .catch()
                }
            }
        },
        // 修改用户基本信息
        set2info: function () {
            axios.patch(this.host + '/user/',
                {
                    address: this.address,
                    mobile: this.mobile,
                    birth_time: this.birth_time,
                    city: this.city,
                    school: this.school,
                    personal_web: this.personal_web,
                    email: this.email,
                    gender: this.gender,
                    real_avatar: this.real_avatar,
                    company: this.company,
                    real_name: this.real_name,

                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    this.set_info = true;
                })
                .catch(error => {
                    alert(error.data)
                })

        }
        ,
        set_skill: function () {
            axios.patch(this.host + '/user/',
                {
                    skills: this.skills
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                })
                .then(response => {
                    alert(this.real_avatar);

                    sets_skill = true
                })
                .catch(error => {
                    alert(error.data)
                })
        },

        setjobs: function () {
            axios.post(this.host + "/experiences/",
                {
                    u_company: this.u_company,
                    u_city: this.u_city,
                    position: this.position,
                    s_time: this.s_time,
                    e_time: this.e_time,
                    u_skills: this.u_skill,
                    desc: this.desc,
                    user_id: this.user_id,

                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                }
            ).then(response => {
                this.ex_id = response.data.id;
                this.u_company = response.u_company;
                this.u_city = response.data.u_city;
                this.position = response.data.position;
                this.s_time = response.data.s_time;
                this.e_time = response.data.e_time;
                this.u_skill = response.data.u_skill;
                this.desc = response.data.desc;
                sets_jobs = true;
            })
                .catch(error => {
                    alert(error.data)
                })

        },

        modify_jobs: function () {
            axios.patch(this.host + "/experiences/id=" + this.ex_id,
                {
                    u_company: this.u_company,
                    u_city: this.u_city,
                    position: this.position,
                    s_time: this.s_time,
                    e_time: this.e_time,
                    u_skills: this.u_skill,
                    desc: this.desc,
                    user_id: this.user_id,
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                }
            ).then(response => {
                this.ex_id = response.data.id;

                this.u_company = response.u_company;
                this.u_city = response.data.u_city;
                this.position = response.data.position;
                this.s_time = response.data.s_time;
                this.e_time = response.data.e_time;
                this.u_skill = response.data.u_skill;
                this.desc = response.data.desc;
                sets_jobs = true;
            })
                .catch(error => {
                    alert(error.data)
                })

        }


        , getjobs: function () {
            axios.get(this.host + "/experiences/experience/?id=" + this.ex_id,

                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    }, responseType: 'json'
                }
            )
                .then(response => {
                    this.u_company = response.data.u_company;
                    this.u_city = response.data.u_city;
                    this.position = response.data.position;
                    this.s_time = response.data.s_time;
                    this.e_time = response.data.e_time;
                    this.u_skill = response.data.u_skills;
                    this.desc = response.data.desc;
                    // this.ex_id=response.data.id;

                })
                .catch(error => {
                    alert(error.data)
                })

        }
        ,
        settoedu: function () {
            axios.post(this.host + "/educations/",
                {
                    u_school: this.u_school,
                    study: this.study,
                    u_education: this.u_education,
                    s_time: this.s_time1,
                    e_time: this.e_time1,
                    u_skills: this.u_skills,
                    desc: this.desc1,
                },

                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                }
            )
                .then(response => {
                    // 返回生成的id;
                    this.ed_id = response.data.id;
                    this.set_educations = true
                })
                .catch(error => {
                    alert(error.data)
                })

        }


        , geteducations: function () {
            axios.get(this.host + "/educations/education/?id=" + this.ed_id,

                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    }, responseType: 'json'
                }
            )
                .then(response => {
                    this.u_school = response.data.u_school;
                    this.study = response.data.study;
                    this.u_education = response.data.u_education;
                    this.s_time1 = response.data.s_time;
                    this.e_time1 = response.data.e_time;
                    this.u_skills = response.data.u_skills;
                    this.desc1 = response.data.desc;
                })
                .catch(error => {
                    alert(error.data)
                });
            //

        }


    },

});